using System;
using System.Collections.Generic;

using Blog.Net.Common.DataObjects;
using Blog.Net.Business.BusinessObjects;

using NHibernate;
using NHibernate.Criterion;
using CureConsulting.DataAccess;

namespace Blog.Net.DataAccess.DataControls
{
	public partial class BlogResponseDataControl : IDataControl<BlogResponseBO>
	{
		#region FieldNames
		
		/// <summary>
        /// Struct to hold field names
        /// </summary>
		public struct FieldNames
        {
			/// <summary>
        	/// HQL Property Name for Id
        	/// </summary>
			public const string Id = "Id";
			/// <summary>
        	/// HQL Property Name for Name
        	/// </summary>
			public const string Name = "Name";
			/// <summary>
        	/// HQL Property Name for Email
        	/// </summary>
			public const string Email = "Email";
			/// <summary>
        	/// HQL Property Name for Comment
        	/// </summary>
			public const string Comment = "Comment";
			/// <summary>
        	/// HQL Property Name for CreateDate
        	/// </summary>
			public const string CreateDate = "CreateDate";
			/// <summary>
        	/// HQL Property Name for BlogEntry
        	/// </summary>
			public const string BlogEntry = "BlogEntry";
		}
		
		#endregion
		
		#region Private Members
		
		private static BlogResponseDataControl blogResponseDataControl;
		private static object lockBlogResponseDataControl = new object();
		private static NHibernateDataControl<BlogResponse> dataControl = new NHibernateDataControl<BlogResponse>();
		
		#endregion
		
		#region Constructors
		
		/// <summary>
		/// Constructor for blog_response.
		/// </summary>
		public BlogResponseDataControl(){}
		
		#endregion
		
		#region Private Methods

        private static IList<BlogResponseBO> ConvertPOCOListToBOList(IList<BlogResponse> blogResponses)
        {
            List<BlogResponseBO> retVal = new List<BlogResponseBO>();

            if (blogResponses != null)
            {
                foreach (BlogResponse blogResponse in blogResponses)
                {
                    retVal.Add(new BlogResponseBO(blogResponse));
                }
            }

            return retVal;
        }

        #endregion
		
		#region Public Properties

        /// <summary>
        ///Singleton Instance
        /// </summary>
        public static BlogResponseDataControl Instance
        {
            get
            {
                lock (lockBlogResponseDataControl)
                {
                    if(blogResponseDataControl == null)
                    {
                        blogResponseDataControl = new BlogResponseDataControl();
                    }
                }

                return blogResponseDataControl;
            }
        }

        #endregion
		
		#region CRUD

        /// <summary>
        /// Function to Save a BlogResponse
        /// </summary>
        /// <param name="blogResponse"></param>
        /// <returns></returns>
        public int? Save(BlogResponseBO blogResponse)
        {
            int? retVal = dataControl.Save(blogResponse.POCO);
            if (retVal.HasValue)
            	dataControl.Refresh(blogResponse.POCO);
            return retVal;
        }

        /// <summary>
        /// Function to Save a list of blog_responses
        /// </summary>
        /// <param name="blogResponses"></param>
        /// <returns></returns>
        public bool Save(IList<BlogResponseBO> blogResponses)
        {
            List<BlogResponse> itemsToSave = new List<BlogResponse>();

            foreach (BlogResponseBO blogResponse in blogResponses)
            {
                itemsToSave.Add(blogResponse.POCO);
            }

            return dataControl.Save(itemsToSave);
        }

        /// <summary>
        /// Function to delete a BlogResponse
        /// </summary>
        /// <param name="blogResponse"></param>
        /// <returns></returns>
        public bool Delete(BlogResponseBO blogResponse)
        {
            return dataControl.Delete(blogResponse.POCO);
        }

        /// <summary>
        /// Function to Delete a list of blog_responses
        /// </summary>
        /// <param name="blogResponses"></param>
        /// <returns></returns>
        public bool Delete(IList<BlogResponseBO> blogResponses)
        {
            List<BlogResponse> itemsToDelete = new List<BlogResponse>();

            foreach (BlogResponseBO blogResponse in blogResponses)
            {
                itemsToDelete.Add(blogResponse.POCO);
            }

            return dataControl.Delete(itemsToDelete);
        }		
		
        #endregion
		
		#region Queries
		
		/// <summary>
        /// Get BlogResponse by ID
        /// </summary>
        /// <param name="id">ID of BlogResponse</param>
        /// <returns>The BlogResponse</returns>
        public BlogResponseBO GetById(int? id)
        {
			BlogResponse retVal = dataControl.GetObjectById(id);
			
			if(retVal != null)
				return new BlogResponseBO(retVal);
			
			return null;
		}
		
		/// <summary>
        /// Return list of all blog_responses
        /// </summary>
        /// <returns></returns>
        public IList<BlogResponseBO> GetAll()
        {
			IList<BlogResponse> retVal = dataControl.GetQueryResult(dataControl.GetCriteria());

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
         
			return new List<BlogResponseBO>();
        }
		
		/// <summary>
        /// Return list of all blog_responses
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="maxResults"></param>
        /// <returns></returns>
        public IList<BlogResponseBO> GetAll(int firstResult, int maxResults)
        {
			IList<BlogResponse> retVal = dataControl.GetQueryResult(dataControl.GetCriteria(), firstResult, maxResults);

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
            
			return new List<BlogResponseBO>();
        }
		
		/// <summary>
        /// Return list of all blog_responses
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="maxResults"></param>
		/// <param name="sortExpression"></param>
        /// <returns></returns>
        public IList<BlogResponseBO> GetAll(int firstResult, int maxResults, string sortExpression)
        {
			ICriteria criteria = dataControl.GetCriteria();

            if (!String.IsNullOrEmpty(sortExpression))
            {
                string[] sort = sortExpression.Split(' ');
                bool ascending = true;
                if (sort.Length > 1 && sort[1].ToUpper() == "DESC") { ascending = false; }
                criteria.AddOrder(new NHibernate.Criterion.Order(sort[0],ascending));
            }
			
			IList<BlogResponse> retVal = dataControl.GetQueryResult(criteria, firstResult, maxResults);

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
        
			return new List<BlogResponseBO>();
        }
		
		/// <summary>
        /// Return count of all blog_responses
        /// </summary>
        /// <returns></returns>
        public int GetCountOfAll()
        {			
			int result = (int)dataControl.GetCriteria().SetProjection(Projections.RowCount()).UniqueResult();
			return result;
        }
		
		/// <summary>
        /// Query to get blog_responses by BlogEntry
        /// </summary>
		/// <param name="blogEntry">BlogEntry of the BlogResponse</param>
		/// <param name="firstResult"></param>
        /// <returns>The blog_responses</returns>
        public IList<BlogResponseBO> GetByBlogEntry(int blogEntry, int firstResult, int maxResults, string sortExpression)
        {
			ICriteria criteria = dataControl.GetCriteria();
            criteria.CreateCriteria(FieldNames.BlogEntry).Add(Expression.Eq(BlogResponseDataControl.FieldNames.Id, blogEntry));
						
			if (!String.IsNullOrEmpty(sortExpression))
            {
                string[] sort = sortExpression.Split(' ');
                bool ascending = true;
                if (sort.Length > 1 && sort[1].ToUpper() == "DESC") { ascending = false; }
                criteria.AddOrder(new Order(sort[0], ascending));
            }
			
			IList<BlogResponse> retVal = dataControl.GetQueryResult(criteria, firstResult, maxResults);
			
			if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
			
			return new List<BlogResponseBO>();
        }
		
		/// <summary>
        /// Return count of all blog_responses by BlogEntry
        /// </summary>
		/// <param name="blogEntry">BlogEntry of the BlogResponse</param>
        /// <returns></returns>
        public int GetCountByBlogEntry(int blogEntry)
        {	
			ICriteria criteria = dataControl.GetCriteria();
            criteria.CreateCriteria(FieldNames.BlogEntry).Add(Expression.Eq(BlogResponseDataControl.FieldNames.Id , blogEntry));
			criteria.SetProjection(Projections.RowCount());
			int result = (int)criteria.UniqueResult();
			
			return result;
        }
		
		#endregion
	}
}